package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.QuestionRequestDTO;
import com.example.demo.mapper.QuestionBankManageMapper;
import com.example.demo.mapper.QuestionManageMapper;
import com.example.demo.mapper.entity.Question;
import com.example.demo.mapper.entity.QuestionBank;
import com.example.demo.service.QuestionManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class QuestionManageServiceImpl extends ServiceImpl<QuestionManageMapper, Question> implements QuestionManageService {
    @Autowired
    private QuestionManageMapper mapper;


    @Override
    public PageVo<Question> getQuestionList(PageBo pageBo) {
        List<Question> logs = mapper.selectList(new QueryWrapper<>());
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<Question> findQuestionBankList(PageBo pageBo, int bankId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if (bankId!= 0) {
            queryWrapper.eq("bank_id", bankId);
        }
        List<Question> logs = mapper.selectList(queryWrapper);
        return new PageVo<>(pageBo, logs);
    }


    @Override
    public List<Question> searchQuestionList(QuestionRequestDTO request) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if (request.getQuestionText() != null) {
            queryWrapper.like("question_text", request.getQuestionText());
        }
        if (request.getBankId() != 0) {
            queryWrapper.eq("bank_id", request.getBankId());
        }
        if (request.getQuestionType() != 0) {
            queryWrapper.eq("question_type", request.getQuestionType());
        }
        List<Question> result = mapper.selectList(queryWrapper);

        return result;
    }

    @Override
    public Result<String> addQuestion(Question request) {
        if(request!=null){
            int addResult=mapper.insert(request);
            int addBankResult=mapper.updateNewBank(request.getBankId());
            if (addResult > 0 && addBankResult > 0) {
                return Result.ok("添加成功");
            } else {
                return Result.fail("添加失败");
            }
        }
        return Result.fail("添加失败,填写内容为空");
    }

    @Override
    public Result<String> updateQuestion(QuestionRequestDTO request) {
        if (request != null && request.getQuestionId() != 0) {
            int result = mapper.updateById(request);
            if (result > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.fail("修改失败");
            }
        } else {
            return Result.fail("参数不正确或未提供主键信息");
        }
    }

    @Override
    public Result<String> deleteQuestion(Question request) {
        QuestionBank nowBank = mapper.selectQuestionBankById(request.getBankId());
        nowBank.setQuestionCount(nowBank.getQuestionCount() - 1);
        if(request!=null){
            if(mapper.deleteById(request)>0 && mapper.updateQuestionBank(nowBank)>0){
                return Result.ok("删除成功");
            }
            else return Result.fail("删除失败");
        }
        return Result.fail("删除失败,选择内容为空");
    }

    @Override
    public Result<String> batchDeleteQuestion(List<Question> requestList) {
        if(requestList!=null){
            int updateQuestionBankResult=0;
            for (Question request : requestList) {
                updateQuestionBankResult = mapper.updateNowBank(request.getBankId());
            }
            int deleteQuestionResult = mapper.batchDeleteQuestion(requestList);
            if(deleteQuestionResult > 0 && updateQuestionBankResult > 0) return Result.ok("批量删除成功");
            return Result.fail("删除失败");
        }
        return Result.fail("删除失败,选择内容为空");
    }

    @Override
    public Result<String> moveQuestionBank(Question request) {
        Question nowQuestion= mapper.selectById(request.getQuestionId());
        int nowBankId = nowQuestion.getBankId(); //題目-1
        int newBankId = request.getBankId();  //題目+1

        nowQuestion.setBankId(request.getBankId());
        if (nowQuestion != null) {
            int changeNowBank = mapper.updateNowBank(nowBankId);
            int changeNewBank = mapper.updateNewBank(newBankId);
            int result = mapper.updateById(nowQuestion);
            if (result > 0 && changeNowBank > 0 && changeNewBank > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.fail("修改失败");
            }
        } else {
            return Result.fail("参数不正确或未提供主键信息");
        }
    }

    public void setRequestAnswer(QuestionRequestDTO request) {
        //如果是單選題
        if(request.getQuestionType()==1){
            if(request.getSingleChoice()==1) request.setCorrectAnswer(8);
            if(request.getSingleChoice()==2) request.setCorrectAnswer(4);
            if(request.getSingleChoice()==3) request.setCorrectAnswer(2);
            if(request.getSingleChoice()==4) request.setCorrectAnswer(1);
        }
        //如果是多選題
        if (request.getQuestionType() == 2) {
            if (request.getMultipleChoice().size() != 0) {
                int sum = request.getMultipleChoice().stream().mapToInt(Integer::intValue).sum();
                request.setCorrectAnswer(sum);
            }
        }
        //如果是判斷題
        if (request.getQuestionType() == 3) {
            if (request.getTrueFalse()==1) request.setCorrectAnswer(8);
            if (request.getTrueFalse()==2) request.setCorrectAnswer(4);
        }

    }
}
